﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Web.Script.Serialization;
using CommonLibrary.Data.Model;
using CommonLibrary.Other;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

namespace CommonLibrary.Data.Json
{
    public static class Jc
    {
        public static string SerializeDataTable(this DataTable dt)
        {
            if (dt == null)
            {
                return null;
            }

            return JsonConvert.SerializeObject(dt, Formatting.Indented);
        }

        public static string SerializeDataTable(this DataTable dt, string timeFormat)
        {
            if (dt == null)
            {
                return null;
            }

            IsoDateTimeConverter timeConverter = new IsoDateTimeConverter();//这里使用自定义日期格式，默认是ISO8601格式
            timeConverter.DateTimeFormat = "yyyy-MM-dd HH:mm:ss";//设置时间格式
            if (timeFormat.ClIsNullOrWhiteSpace() == false)
            {
                timeConverter.DateTimeFormat = timeFormat;
            }

            return JsonConvert.SerializeObject(dt, Formatting.Indented, timeConverter);
        }


        public static DataTable DeserializeDataTable(string jsonText)
        {
            return JsonConvert.DeserializeObject<DataTable>(jsonText);
        }

        /// <summary>
        /// DataTable生成Json
        /// </summary>
        /// <param name="in_dt"></param>
        /// <returns></returns>
        public static string ClToJson(this DataTable in_dt)
        {
            if (in_dt == null)
            {
                return null;
            }

            return JsonConvert.SerializeObject(in_dt, Formatting.Indented);
        }

        public static string ClToJson(this DataTable in_dt, string timeFormat)
        {
            if (in_dt == null)
            {
                return null;
            }

            return SerializeDataTable(in_dt, timeFormat);
        }

        /// <summary>
        /// Json生成DataTable
        /// </summary>
        /// <param name="in_dt"></param>
        /// <param name="jsonText"></param>
        /// <returns></returns>
        public static DataTable ClFromJson(this DataTable in_dt, string jsonText)
        {
            in_dt = JsonConvert.DeserializeObject<DataTable>(jsonText);
            return in_dt;
        }


        /// <summary>
        /// 格式化Json字符串
        /// </summary>
        /// <param name="jsonText"></param>
        /// <returns></returns>
        public static string JsonFormatString(string jsonText)
        {
            DynamicJsonObject dynamicJsonObject = CommonLibrary.Data.JSS.ConvertJsonToDynamic(jsonText);

            string jsonFormat = JsonConvert.SerializeObject(dynamicJsonObject, Formatting.Indented);
            jsonFormat = jsonFormat.Replace("{\r\n" + @"  ""Dictionary"": {", "  {");
            jsonFormat = jsonFormat.Substring(0, jsonFormat.Length - 3);
            return jsonFormat;
        }


        /// <summary>
        /// 将Http Get地址栏字符变为Json
        /// </summary>
        /// <param name="in_url"></param>
        /// <param name="urlDecode">是否解码</param>
        /// <returns></returns>
        public static string ClHttpGetToJson(this string in_url, bool urlDecode, Encoding encoding)
        {
            string str = in_url;
            if (str.Contains("?"))
            {
                str = Regex.Match(str, @"(?<=\?).*", RegexOptions.None).ToString();
            }
            if (urlDecode)
            {
                if (encoding == null)
                {
                    str = str.ClUrlDecode();
                }
                else
                {
                    str = str.ClUrlDecode(encoding);
                }

            }

            string[] arr = str.Split('&');
            Dictionary<string, string> dictionary = new Dictionary<string, string>();
            foreach (string item in arr)
            {
                string[] arrPara = item.Split('=');
                dictionary.Add(arrPara[0], arrPara[1]);
            }

            string result = JsonConvert.SerializeObject(dictionary, Formatting.Indented);
            return result;
        }

        /// <summary>
        /// 将Http Get地址栏字符变为Json，默认不解码
        /// </summary>
        /// <param name="in_url"></param>
        /// <returns></returns>
        public static string ClHttpGetToJson(this string in_url, Encoding encoding)
        {
            return in_url.ClHttpGetToJson(true, encoding);
        }

        /// <summary>
        /// 将Http Get地址栏字符变为Json
        /// </summary>
        /// <param name="in_url"></param>
        /// <returns></returns>
        public static string ClHttpGetToJson(this string in_url)
        {
            return in_url.ClHttpGetToJson(false, null);
        }


        public static string SerializeObjectToJson(object obj)
        {
            if (obj == null)
            {
                return null;
            }

            return JsonConvert.SerializeObject(obj, Formatting.Indented);
        }

        public static string SerializeObjectToJson(object obj, string timeFormat)
        {
            if (obj == null)
            {
                return null;
            }

            IsoDateTimeConverter timeConverter = new IsoDateTimeConverter();//这里使用自定义日期格式，默认是ISO8601格式
            timeConverter.DateTimeFormat = "yyyy-MM-dd HH:mm:ss";//设置时间格式
            if (timeFormat.ClIsNullOrWhiteSpace() == false)
            {
                timeConverter.DateTimeFormat = timeFormat;
            }

            return JsonConvert.SerializeObject(obj, Formatting.Indented, timeConverter);
        }

        public static T DeserializeObjectFromJson<T>(string jsonText)
        {
            return JsonConvert.DeserializeObject<T>(jsonText);
        }


        public static string ClToJson(this ModelResult modelResult)
        {
            return SerializeObjectToJson(modelResult);
        }

        public static ModelResult ClToModelResult(this string jsonText)
        {
            return DeserializeObjectFromJson<ModelResult>(jsonText);
        }
    }
}
